/**
 * 根据配置执行SQL语句
 * 
 * @author 迟胜强
 * @date 2018-9-12
 * 
 */
package aorey.database;


import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.sql.DataSource;
import aorey.database.config.DataConfigDelete;
import aorey.database.config.DataConfigField;
import aorey.database.config.DataConfigInsert;
import aorey.database.config.DataConfigSelect;
import aorey.database.config.DataConfigUpdate;
import aorey.nosql.EhcacheUtil;
import aorey.notice.Message;

public class DataBaseConfigAction extends DataBaseFactory {

	protected HttpServletRequest request;
	protected DataConfig dataConfig;
	protected EhcacheUtil framework;
	
	public DataBaseConfigAction(HttpServletRequest request)
	{
		// WEB请求
		this.request = request;
		// 实例化配置
		this.dataConfig = new DataConfig(request);
		// 获取框架注入的所有数据
		this.framework = (EhcacheUtil) request.getServletContext().getAttribute("aorey");
	}
	
	/**
	 * 根据配置文件读取数据
	 * @param id	配置文件ID
	 * @return		返回标准消息数据结构(Message)
	 */
	@SuppressWarnings("unchecked")
	public Message select(String id)
	{
		Message msg = new Message();
		try {
			DataConfigSelect dataConfigSelect = this.dataConfig.getSelect(id);
			DataTag dataTag = new DataTag(request);
			HashMap<String, DataSource> dataBaseDrivers = (HashMap<String, DataSource>) framework.get("database", "drivers");
			DataBaseFactory db = new DataBaseFactory();
			db.getConnection(dataBaseDrivers.get(dataConfigSelect.data_source).getConnection());
			String rs = db.getResult(dataTag.tagReplace(dataConfigSelect.sql));
			if (!rs.equals("")||db.getError()!=null) {
				msg.data = rs;
			}
			else
			{
				// 数据错误
				msg.state = "10002";
				msg.error =db.getError();
			}
			db.close();
		} catch (Exception e) {
			// 配置文件或连接池错误
			msg.state = "10001";
			msg.error = e.getMessage();
		}
		return msg;
	}
	
	
	/**
	 * 根据配置文件写入数据
	 * @param id	配置文件ID
	 * @return		返回标准消息数据结构(Message)
	 */
	@SuppressWarnings("unchecked")
	public Message insert(String id)
	{
		Message msg = new  Message();
		try {
			DataConfigInsert dataConfigInsert = this.dataConfig.getInsert(id);
			List<DataConfigField> fields  = dataConfigInsert.fields;
			int fieldSize = fields.size();
			Table table = new Table(dataConfigInsert.name);
			DataCheck dataCheck = (DataCheck) this.framework.get("config", "datatype");
			String vString;
			for(int i=0;i<fieldSize;i++)
			{
				
				
				vString = dataCheck.getFormValue(request, fields.get(i).scope, fields.get(i).id);
				if(!dataCheck.checkValue(vString, fields.get(i).type, fields.get(i).allow_null, fields.get(i).len))
				{
					msg.state = "10003";
					msg.error = fields.get(i).errortext;
					return msg;
				}
				table.setField(fields.get(i).name, vString);
			}
			String insertSQL = table.insert();
			DataBaseFactory db = new DataBaseFactory();
			HashMap<String, DataSource> dataBaseDrivers = (HashMap<String, DataSource>) framework.get("database", "drivers");
			db.getConnection(dataBaseDrivers.get(dataConfigInsert.data_source).getConnection());
			int rs = db.doSQL(insertSQL);
			db.close();
			if (rs == -1) {
				msg.state = "10002";
				msg.error = db.getError();
				return msg;
			}	
		} catch (Exception e) {
			msg.state = "10001";
			msg.error = e.getMessage();
		}
		return msg;
	}
	
	/**
	 * 根据配置文件修改数据
	 * @param id	配置文件ID
	 * @return		返回标准消息数据结构(Message)
	 */
	@SuppressWarnings("unchecked")
	public Message update(String id)
	{
		Message msg = new Message();
		try
		{
			DataConfigUpdate dataConfigUpdate = this.dataConfig.getUpdate(id);
			DataTag dataTag = new DataTag(request);
			List<DataConfigField> fields = dataConfigUpdate.fields;
			int fieldSize = fields.size();
			Table table = new Table(dataConfigUpdate.name);
			DataCheck dataCheck = (DataCheck) this.framework.get("config", "datatype");
			String vString;
			for(int i=0;i<fieldSize;i++)
			{
				vString = dataCheck.getFormValue(request, fields.get(i).scope, fields.get(i).id);
				if(!dataCheck.checkValue(vString, fields.get(i).type, fields.get(i).allow_null, fields.get(i).len))
				{
					msg.state = "10003";
					msg.error = fields.get(i).errortext;
					return msg;
				}
				table.setField(fields.get(i).name, vString);
			}
			
			String updateSQL =dataTag.tagReplace(table.update(dataConfigUpdate.where));
			DataBaseFactory db = new DataBaseFactory();
			HashMap<String, DataSource> dataBaseDrivers = (HashMap<String, DataSource>) framework.get("database", "drivers");
			db.getConnection(dataBaseDrivers.get(dataConfigUpdate.data_source).getConnection());
			int rs = db.doSQL(updateSQL);
			if (rs == -1) {
				msg.state = "10002";
				msg.error = db.getError();
				return msg;
			}
			db.close();
		}
		catch (Exception e) {
			msg.state = "10001";
			msg.error = e.getMessage();
		}
		return msg;
	}
	
	
	/**
	 * 根据配置删除数据
	 * @param id	配置文件ID
	 * @return		返回标准消息数据结构(Message)
	 */
	@SuppressWarnings("unchecked")
	public Message delete(String id)
	{
		Message msg = new Message();
		try {
			DataConfigDelete dataConfigDelete = this.dataConfig.getDelete(id);
			DataTag dataTag = new DataTag(request);
			System.out.println(dataConfigDelete.sql);
			HashMap<String, DataSource> dataBaseDrivers = (HashMap<String, DataSource>) framework.get("database", "drivers");
			DataBaseFactory db = new DataBaseFactory();
			db.getConnection(dataBaseDrivers.get(dataConfigDelete.data_source).getConnection());
			System.out.println("SQL:"+dataTag.tagReplace(dataConfigDelete.sql));
			int rs = db.doSQL(dataTag.tagReplace(dataConfigDelete.sql));
			System.out.println(rs);
			if (rs==-1)
			{
				// 数据错误
				msg.state = "10002";
				msg.error =db.getError();
			}
			db.close();
		} catch (Exception e) {
			// 配置文件或连接池错误
			msg.state = "10001";
			msg.error = e.getMessage();
		}
		return msg;
	}
}
